package view;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * This Email folderNode class will display nodes in the hierarchy having tree structure.
 *
 */
public class EmailFolderNode extends DefaultMutableTreeNode implements EmailFolderNodeImp
{

	private static final long serialVersionUID = -6995466093343914745L;
	
	private String node_name;
	
	private String persistenceFilePath = "";
	
	/**
     *Constructor for class EmailfolderNode having name of the folder as string
	 * @param name the folder node name
	 */
	public EmailFolderNode(String name)
    {
        super(name);
		this.node_name = name;
    }

	/*
	 * (non-Javadoc)
	 * @see view.EmailFolderNodeImp#getFolderName()
	 */
	public String getFolderName()
	{
		return node_name;
	}
	
	/**
     * Set path for user file in persistence
	 * @param persistenceFilePath the persistence file path
	 */
	public void setPersistenceFilePath(String persistenceFilePath)
	{
	    this.persistenceFilePath = persistenceFilePath;
	}
	
	/*
	 * (non-Javadoc)
	 * @see view.EmailFolderNodeImp#getPersistenceFilePath()
	 */
	public String getPersistenceFilePath()
    {
        return this.persistenceFilePath;
    }
	
	
	/*
	 * (non-Javadoc)
	 * @see view.EmailFolderNodeImp#getFolders()
	 */
	public List<EmailFolderNodeImp> getFolders()
	{
	    int numberOfChild = this.getChildCount();
	    List<EmailFolderNodeImp> folders = new LinkedList<EmailFolderNodeImp>();
	    for(int j=0; j<numberOfChild;j++)
	    {
	        EmailFolderNodeImp node = (EmailFolderNodeImp) this.getChildAt(j);
	        folders.add(node);
	    }
	    
	    return folders;
	}
	
	/**
     * load persistence folders
     */
    public void loadPersistenceFolders()
    { 
        this.removeAllChildren();
        
        File persistentFile = new File(persistenceFilePath);
        
        if(persistentFile.isDirectory())
        {
            File[] listOfFiles = persistentFile.listFiles(); 
            
            if(listOfFiles == null)
            {
                return;
            }
            
            for(File file : listOfFiles)
            {
                 if (file.isDirectory() && !file.getName().endsWith(".svn")) 
                 {
                     EmailFolderNode emailFolderNode = new EmailFolderNode(file.getName());
                     emailFolderNode.setPersistenceFilePath(file.getPath());
                     this.add(emailFolderNode);
                     this.iteratePersistenceFolders(file, emailFolderNode);
                 }
            }
        }
    }
    
    /**
     * iterate persistence folders
     * @param folder the folder to iterate
     * @param emailFolderNode the email folder node
     */
    private void iteratePersistenceFolders(File folder,EmailFolderNode emailFolderNode)
    {
        File[] listOfFiles = folder.listFiles(); 
        for(File file : listOfFiles)
        {
             if (file.isDirectory() && !file.getName().endsWith(".svn")) 
             {
                 EmailFolderNode newEmailFolderNode = new EmailFolderNode(file.getName());
                 newEmailFolderNode.setPersistenceFilePath(file.getPath());
                 emailFolderNode.add(newEmailFolderNode);
                 this.iteratePersistenceFolders(file,newEmailFolderNode);
             }
        }
    }

}
